//
//  FoodData.cpp
//  WeddingFoodMaker
//
//  Created by luotianqiang1 on 16/3/1.
//
//

#include "FoodData.h"
#include "CategoryReader.h"
using namespace std;
using namespace cocos2d;
std::unordered_map<std::string,cocos2d::Vector<cocos2d::Node*>> FoodData::decorations;
std::string FoodData::flavor = "grape";

std::vector<std::string> FoodData::flavorNames = {
    "cola","love","kiwi","perfume","orange",
    "magic","blue-raspberry","passion","strawberry","rainbow","grape"
};


const std::vector<std::string>& FoodData::getAllFlavor(){
    return flavorNames;
}



void FoodData::setFlavor(const string&  temp){
    flavor = temp;
}

const string&  FoodData::getFlavor(){
    return flavor;
}

std::string FoodData::getChooseNext(){
    string nextStepName = "BlenderLayer";
    vector<vector<string>> chooseNextConfigs = {
        {"CutFruitLayer","strawberry","orange","kiwi"},
        {"TouchFallLayer","blue-raspberry","grape"},
        {"OpenLidLayer","cola"},
        {"MakeIce","love","perfume","magic","passion","rainbow"}
    };
    for(auto config:chooseNextConfigs) {
        auto itor = std::find(config.begin()+1, config.end(), flavor);
        if(itor != config.end())
            nextStepName = *config.begin();
    }
    return nextStepName;
}

std::vector<std::string> FoodData::getCutPath(){
    std::vector<std::string> temps;
    int num =0;
    if(flavor == "kiwi")
        num = 2;
    else if(flavor == "orange")
        num = 2;
    else if(flavor == "strawberry")
        num = 4;
    for(int i=0;i<num;i++)
        temps.push_back("cutLayer/Cut"+flavor+".csb");
    return temps;
}

 std::vector<std::string> FoodData::getFallThings(){
     std::vector<std::string> temps;
     std::vector<std::string> fruitFlavor = {"kiwi","orange","strawberry","blue-raspberry","grape"};
     auto itor =std::find(fruitFlavor.begin(), fruitFlavor.end(), flavor);
     if(itor != fruitFlavor.end()){
         temps = {"ice","fruit"};
     }else if(flavor == "cola"){
         temps = {"ice","cola"};
     }else {
         temps = {"ice","water","color"};
     }
     return temps;
}

std::vector<std::string> FoodData::getColorNames(){
    std::vector<std::string> temps;
    vector<vector<string>> colorConfigs = {
        {"love","red","green","orange","blue"},
        {"perfume","cyan","blue","red","yellow"},
        {"magic","green","cyan","blue","purple"},
        {"passion","red","blue","green","yellow","purple"},
        {"rainbow","cyan","blue","red","yellow","green"}
    };
    for(auto config:colorConfigs){
        if(config[0] == flavor){
            temps.insert(temps.end(), config.begin()+1,config.end());
            break;
        }
    }
    return temps;
}

std::vector<std::string> FoodData::getMixPath(){
    std::vector<std::string> temps;
    std::vector<std::string> fruitFlavor = {"kiwi","orange","strawberry","blue-raspberry","grape"};
    temps.push_back("PNG/5/Slushy_animation/"+flavor+"2.png");
    auto itor =std::find(fruitFlavor.begin(), fruitFlavor.end(), flavor);
    if(itor != fruitFlavor.end())
        temps.push_back("PNG/5/Slushy_animation/"+flavor+"3.png");
    return temps;
}

 std::vector<std::string> FoodData::getFallPath(){
     std::vector<std::string> temps;
     std::vector<std::string> notColors = {"cola","kiwi","orange","strawberry","blue-raspberry","grape"};
     auto itor = find(notColors.begin(), notColors.end(), flavor);
     if(itor != notColors.end()){
         temps.push_back("PNG/fallsprite/"+flavor+"0.png");
         temps.push_back("PNG/fallsprite/"+flavor+"1.png");
     }else {
         temps.push_back("PNG/fallsprite/raspberry0.png");
         temps.push_back("PNG/fallsprite/raspberry1.png");
     }
     return temps;
}
 std::vector<std::string> FoodData::getInCupPath(){
    std::vector<std::string> temps;
     temps.push_back("PNG/6/"+flavor+"_down.png");
     temps.push_back("PNG/6/"+flavor+"_on.png");
     return temps;
}

std::vector<std::string> FoodData::getDecorationPath(const std::string& name,int index){
    std::vector<std::string> temps;
    if(name == "sprinkle"){
        SpriteFrameCache::getInstance()->addSpriteFramesWithFile("PNG/6/sprinkle/Plist.plist");
        auto path = StringUtils::format("PNG/6/sprinkle/sprinkle%d.png",index);
        if(SpriteFrameCache::getInstance()->getSpriteFrameByName(path))
            temps.push_back(path);
        for(int i=1;i<10;i++){
            auto path = StringUtils::format("PNG/6/sprinkle/sprinkle%d_%d.png",index,i);
            if(SpriteFrameCache::getInstance()->getSpriteFrameByName(path))
                temps.push_back(path);
            else
                break;
        }
    }else if(name == "cup"){
        temps.push_back(StringUtils::format("PNG/6/decorative pattern/%d.png",index));
    }else {
        temps.push_back(StringUtils::format("PNG/6/%s/%s%d.png",name.c_str(),name.c_str(),index));
    }
    return temps;
}

std::vector<std::string> FoodData::getIconPath(const std::string& name){
    std::vector<std::string> temps;
    
    temps.push_back("ui/decorate/no.png");
    auto rootPath = "PNG/6/icon/"+name;
    auto num = FilePathUtil::getFileNum(rootPath);
    for(int i=0;i<num;i++){
        auto path = StringUtils::format("%s/%s%d.png",rootPath.c_str(),name.c_str(), i);
        temps.push_back(path);
    }
    
    return temps;
}

void FoodData::clearDecoration(){
    decorations.clear();
}
void FoodData::eraseDecoration(const std::string& key){
    auto itor = decorations.find(key);
    if(itor != decorations.end())
        decorations.erase(itor);
}
void FoodData::addDecoration(const std::string& key,Node* temp){
    auto itor = decorations.find(key);
    if(itor == decorations.end()){
        Vector<Node*> nodes;
        nodes.pushBack(temp);
        decorations.insert(decltype(decorations)::value_type(key,nodes));
    }else
        itor->second.pushBack(temp);
}

cocos2d::Vector<cocos2d::Node*>  FoodData::getDecoration(const std::string& key){
    auto itor = decorations.find(key);
    if(itor != decorations.end())
        return itor->second;
    else
        return Vector<cocos2d::Node*>();
}
